package com.erpanderp.wam.ac.view.reimbursement;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import com.erpanderp.wam.ac.model.entity.reimbursement.Currency;
import com.erpanderp.wam.ac.model.entity.reimbursement.MiscExpense;
import com.erpanderp.wam.ac.model.entity.reimbursement.MiscExpenseDetails;
import com.erpanderp.wam.ac.model.service.reimbursement.MiscReimbursementDAO;
import com.erpanderp.wam.adm.model.entity.CustomLookup;
import com.erpanderp.wam.adm.model.entity.LookupValue;
import com.erpanderp.wam.adm.model.service.CustomLookupDAO;
import com.erpanderp.wam.util.CommonLookup;


@ManagedBean
@ViewScoped
public class ViewMiscReimbursementBean implements Serializable{

	
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	private ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
	private TimeZone timeZone = TimeZone.getDefault();
	private MiscExpense miscExpense = new MiscExpense();
	
	private MiscReimbursementDAO miscReimbursementDAO = new MiscReimbursementDAO();
	
	private List<MiscExpenseDetails> miscExpensesDetailsList = new ArrayList<MiscExpenseDetails>();
	private Map<String,String> currencyMap = new HashMap<String, String>();
	private CustomLookup customLookUp = new CustomLookup();
	private CustomLookupDAO customLookupDAO = new CustomLookupDAO();
	private List<LookupValue> lookUpValuesList = new ArrayList<LookupValue>();
	private List<Currency> Currencies  =  new ArrayList<Currency>();
	
	public ViewMiscReimbursementBean() {
		
		String miscExpenseCode = request.getParameter("miscExpenseCode");  
		miscExpense = miscReimbursementDAO.getMiscExpense(miscExpenseCode);
		
		if(miscExpense.getApprovalComment() == null || miscExpense.getApprovalComment().equals("")){
			miscExpense.setApprovalComment("Pending...");
		}
		
		miscExpensesDetailsList.addAll(miscExpense.getMiscExpensesDetails());
		
		customLookUp = customLookupDAO.getCustomLookUp(CommonLookup.CURRENCY_CUSTOM_LOOKUP);
		lookUpValuesList = customLookupDAO.getLookupValuesList(customLookUp.getCustomLookId());
		totalAmountandCurrencyList();
		
	}
	
	// Total Amount LIST & Currency
	public void totalAmountandCurrencyList() {
		for (LookupValue currency : lookUpValuesList) {
			for (MiscExpenseDetails miscExpenseDetails : miscExpensesDetailsList) {
				if (miscExpenseDetails.getCurrency().equals(
						currency.getLookupValue())) {
					// MATCHED = true;
					boolean ADDED = false;
					for (Currency cur : Currencies) {
						if (currency.getLookupValue().equals(cur.getCurrency())) {
							ADDED = true;
							// change the value
							cur.setAmount(cur.getAmount()
									+ miscExpenseDetails.getAmount());
							break;
						}
					}
					if (!ADDED) {
						// create new currentcy and add to the list
						Currency currencyObj = new Currency();
						currencyObj.setAmount(miscExpenseDetails
								.getAmount());
						currencyObj.setCurrency(miscExpenseDetails
								.getCurrency());
						Currencies.add(currencyObj);
					}
					// break;
				}
			}
		}
	}
	   
	
//GETTERS AND SETTERS
	
	public MiscExpense getMiscExpense() {
		return miscExpense;
	}

	public void setMiscExpense(MiscExpense miscExpense) {
		this.miscExpense = miscExpense;
	}

	public List<MiscExpenseDetails> getMiscExpensesDetailsList() {
		return miscExpensesDetailsList;
	}

	public void setMiscExpensesDetailsList(List<MiscExpenseDetails> miscExpensesDetailsList) {
		this.miscExpensesDetailsList = miscExpensesDetailsList;
	}

	public Map<String,String> getCurrencyMap() {
		return currencyMap;
	}

	public void setCurrencyMap(Map<String,String> currencyMap) {
		this.currencyMap = currencyMap;
	}

	public CustomLookup getCustomLookUp() {
		return customLookUp;
	}

	public void setCustomLookUp(CustomLookup customLookUp) {
		this.customLookUp = customLookUp;
	}

	public CustomLookupDAO getCustomLookupDAO() {
		return customLookupDAO;
	}

	public void setCustomLookupDAO(CustomLookupDAO customLookupDAO) {
		this.customLookupDAO = customLookupDAO;
	}

	public List<Currency> getCurrencies() {
		return Currencies;
	}

	public void setCurrencies(List<Currency> currencies) {
		Currencies = currencies;
	}

	public TimeZone getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}

}
